
package jwww.util;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class TextReader
{
  TextReader()
  {}

  public static List<String> readLines(File file)
    throws FileNotFoundException
  {
    BufferedReader reader =
      new BufferedReader(new FileReader(file));
    return readLines(reader);
  }

  public static List<String> readLines(String s)
  {
    BufferedReader reader =
      new BufferedReader(new StringReader(s));
    return readLines(reader);
  }

  public static List<String> readLines(BufferedReader reader)
  {
    List<String> result = new ArrayList<String>();
    try
    {
      String prevline = "";
      String line = "";
      while ((line = reader.readLine()) != null)
      {
        int hash = line.indexOf("#");
        if (hash >= 0)
          line = line.substring(0,hash);
        line = spaces(line);
        line = (prevline + " " + line).trim();
        if (line.endsWith("\\"))
        {
          line = line.substring(0, line.length()-2);
          prevline = line;
          continue;
        }
        else
        {
          prevline = "";
          line = line.trim();
          if (line.length() > 0)
            result.add(line);
        }
      }
    }
    catch (IOException e)
    {
      System.out.println("ScriptReader: I/O problem.");
      return null;
    }
    return result;
  }

  public static String read(File file)
  throws FileNotFoundException
  {
    BufferedReader reader =
        new BufferedReader(new FileReader(file));
    return read(reader);
  }

  public static String read(String s)
  {
    BufferedReader reader =
        new BufferedReader(new StringReader(s));
    return read(reader);
  }

  public static String read(BufferedReader reader)
  {
    StringBuilder sb = new StringBuilder();
    try
    {
      String line = "";
      while ((line = reader.readLine()) != null)
        sb.append(line);
    }
    catch (IOException e)
    {
      e.printStackTrace();
      System.exit(1);
    }
    return sb.toString();
  }

  public static String spaces(String line)
  {
    String result = "";
    for (int i = 0; i < line.length(); i++)
    {
      if (line.charAt(i)   == '=' &&
          line.charAt(i+1) != '=' &&
          line.charAt(i-1) != '=')
      {
        result += " = ";
      }
      else
        result += line.substring(i,i+1);
    }
    return result;
  }

  public static String[] tokenize(String line)
  {
    if (line == null)
      return null;
    List<String> words = new ArrayList<String>();
    String[] ws = line.split("\\s");
    for (int i = 0; i < ws.length; i++)
      if (ws[i].length() > 0)
        words.add(ws[i]);
    String[] result = new String[words.size()];
    for (int i = 0; i < words.size(); i++)
      result[i] = (String) words.get(i);
    return result;
  }

  public static List<String> list(String line)
  {
    List<String>   result = new ArrayList<String>();
    String[] tokens = tokenize(line);
    for (int i = 0; i < tokens.length; i++)
      result.add(tokens[i]);
    return result;
  }
}
